<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="oldMan" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="40">
      </el-table-column>
      <el-table-column prop="oldmanName" label="老人姓名" width="80"></el-table-column>
      <el-table-column prop="sex" label="性别" width="60">
        <!-- scpoe : 它能获取到当前行的数据-->
        <template scope="scope">
          <span style="color: #1f1e1e" v-if="scope.row.sex === 35">男</span>
          <span style="color: #141212" v-else-if="scope.row.sex === 36">女</span>
          <span style="color: #5e7382" v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column prop="physicalCondition" label="身体状况" width="80">
        <!-- scpoe : 它能获取到当前行的数据-->
        <template scope="scope">
          <span style="color: #4bb814" v-if="scope.row.physicalCondition === 30">较好</span>
          <span style="color: #00aaff" v-else-if="scope.row.physicalCondition === 31">正常</span>
          <span style="color: #e05858" v-else-if="scope.row.physicalCondition === 32">较差</span>
          <span style="color: #5e7382" v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column prop="idcard" label="身份证" width="180"></el-table-column>
      <el-table-column prop="homeAddress" label="家庭地址" width="120"></el-table-column>
      <el-table-column prop="checkInTime" label="入院时间" width="160"></el-table-column>
      <el-table-column prop="dischargeTime" label="出院时间" width="160"></el-table-column>
      <el-table-column prop="bed.bedNumber" label="床位编号" width="100"></el-table-column>
      <el-table-column prop="nurse.nurseNumber" label="护工编号" width="100"></el-table-column>
      <el-table-column prop="state" label="老人状态" width="80">
        <!-- scpoe : 它能获取到当前行的数据-->
        <template scope="scope">
          <span style="color: #11b95c" v-if="scope.row.state === 33">住院中</span>
          <span style="color: #00aaff" v-else-if="scope.row.state === 34">已出院</span>
          <span style="color: #ffd000" v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"  width="150">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :current-page="currentPage"
                     :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增或修改 界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false" width="80%" hight="60%">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules"
               ref="editForm" :inline="true" :before-close="disableClose">
        <el-table-column prop="id" label="员工id" width="60">
        </el-table-column>
          <el-form-item label="老人姓名" prop="oldmanName" style="width: 330px">
            <el-input v-model="editForm.oldmanName" auto-complete="off"></el-input>
          </el-form-item>
        <el-form-item label="老人性别" prop="sex" style="width: 330px">
          <el-select v-model="editForm.sex" placeholder="请选择">
            <el-option
                v-for="item in oldManSex"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老人年龄" prop="age" style="width: 330px">
          <el-input v-model="editForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="身体状况" prop="physicalCondition" style="width: 330px">
          <el-select v-model="editForm.physicalCondition" placeholder="请选择">
            <el-option
                v-for="item in oldManPhysical"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老人户籍" prop="householdRegistration" style="width: 300px">
          <el-input v-model="editForm.householdRegistration" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idcard" style="width: 300px">
          <el-input v-model="editForm.idcard" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="亲属" prop="familyMembers" style="width: 300px">
          <el-input v-model="editForm.familyMembers" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="亲属电话" prop="familyContactNumber" style="width: 300px">
          <el-input v-model="editForm.familyContactNumber" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="家庭住址" prop="homeAddress" style="width: 300px">
          <el-input v-model="editForm.homeAddress" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="入住时间" prop="checkInTime" style="width: 330px">
          <el-date-picker clearable
                          v-model="editForm.checkInTime"
                          type="date"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择入住时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出院时间" prop="dischargeTime" style="width: 330px">
          <el-date-picker clearable
                          v-model="editForm.dischargeTime"
                          type="date"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择出院时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="床位分配" prop="bedId">
          <el-select v-model="editForm.bedId" placeholder="请选择">
            <el-option
                v-for="item in beds"
                :label="item.bedRemarks"
                :value="item.id">
              <span style="float: left">{{ item.bedRemarks }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.bedNumber }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="护工分配" prop="nurseId">
          <el-select v-model="editForm.nurseId" placeholder="请选择">
            <el-option
                v-for="item in nurses"
                :label="item.nurseName"
                :value="item.id">
              <span style="float: left">{{ item.nurseName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.nurseNumber }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false" >取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import {
  getNurseListPage,
  removeNurse,
  batchRemoveNurse,
  editNurse,
  addNurse
} from '../../api/api';

export default {
  data() {
    return {
      options: [],
      title: '',
      filters: {
        keyword: ''
      },
      oldMan: [],
      oldManSex: [],
      oldManPhysical: [],
      beds: [],
      nurses: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      page: 1,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        age: [
          {required: true, message: '请输入年龄', trigger: 'blur'}
        ],
        bedId: [
          {required: true, message: '请选择床位', trigger: 'blur'}
        ],
        checkInTime: [
          {required: true, message: '请选择入院时间', trigger: 'blur'}
        ],
        dischargeTime: [
          {required: true, message: '请选择出院时间', trigger: 'blur'}
        ],
        familyContactNumber: [
          {required: true, message: '请输入家属联系电话', trigger: 'blur'}
        ],
        familyMembers: [
          {required: true, message: '请输入亲属名字', trigger: 'blur'}
        ],
        homeAddress: [
          {required: true, message: '请输入家庭住址', trigger: 'blur'}
        ],
        householdRegistration: [
          {required: true, message: '请输入籍贯', trigger: 'blur'}
        ],
        idcard: [
          {required: true, message: '请输入身份证号', trigger: 'blur'}
        ],
        nurseId: [
          {required: true, message: '请选择护工', trigger: 'blur'}
        ],
        oldmanName: [
          {required: true, message: '请输入老人姓名', trigger: 'blur'}
        ],
        physicalCondition: [
          {required: true, message: '请选择身体状况', trigger: 'blur'}
        ],
        sex: [
          {required: true, message: '请选择老人性别', trigger: 'blur'}
        ],
        state: [
          {required: true, message: '请输入合同编号', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      editForm: {
        age: null,
        bedId: null,
        checkInTime: null,
        createTime: null,
        dischargeTime: null,
        familyContactNumber: '',
        familyMembers: '',
        homeAddress: '',
        householdRegistration: '',
        id: null,
        idcard: null,
        nurseId: null,
        oldmanName: '',
        physicalCondition: '',
        sex: 1,
        state: null,
        bed: null,
        nurse: null,
      },

    }
  },
  methods: {
    disableClose() {
      this.editForm = {};
      this.$refs['editForm'].clearValidate();
      this.editFormVisible = false;
    },
    // 获取床位信息
    getBeds() {
      this.$http.get("/bed")
          .then(result => {
            this.beds = result.data;
          })
    },
    // 获取护工信息
    getNurses() {
      this.$http.get("/nurse/normalNurse")
          .then(result => {
            this.nurses = result.data;
          })
    },
    // 获取老人性别 35=男 36=女
    getOldManSex() {
      this.$http.get("/detail/findOldManSex")
          .then(result => {
            this.oldManSex = result.data;
          })
    },
    // 获取老人身体状况
    getOldManPhysical() {
      this.$http.get("/detail/findOldManPhysical")
          .then(result => {
            this.oldManPhysical = result.data;
          })
    },
    search() {
      this.currentPage = 1;
      this.getOldMan();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getOldMan();
    },
    //获取老人列表
    getOldMan() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.keyword};
      this.listLoading = true;
      //NProgress.start();
      this.$http.post("/oldman/list", para)
          .then(result => {
            // 关闭忙等
            this.listLoading = false;
            this.total = result.data.totals;
            this.oldMan = result.data.rows;
          })
          .catch(res => {
            this.$message({
              message: "网络不通！",
              type: "error",
            })
          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let id = row.id;
        this.$http.delete("/oldman/" + id)
            .then(result => {
              this.listLoading = false;
              if (result.data.success) { // 如果后端返回success
                this.$message({
                  message: result.data.msg,
                  type: 'success'
                });
                this.currentPage = 1,
                    // 刷新列表
                    this.getOldMan();
              } else {
                this.$message({
                  message: result.data.msg,
                  type: 'error'
                });
              }
            }).catch(result => {
          // 1.关闭忙等框
          this.listLoading = false;
          // 2.给出自定义失败提示！
          this.$message({
            message: '系统繁忙，请重试！',
            type: 'error'
          });
        })
      }).catch(() => {
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "编辑";
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.title = "新增";
      this.editFormVisible = true;
      this.editForm = {
        age: null,
        bedId: null,
        checkInTime: null,
        createTime: null,
        dischargeTime: null,
        familyContactNumber: '',
        familyMembers: '',
        homeAddress: '',
        householdRegistration: '',
        id: null,
        idcard: null,
        nurseId: null,
        oldmanName: '',
        physicalCondition: '',
        sex: null,
        state: null,
      };
    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true; // 开启提交按钮忙等
            let para = Object.assign({}, this.editForm); // 表单数据拷贝
            // 1.发送Axios请求
            this.$http.put("/oldman", para)
                .then(result => {
                  console.log(result);
                  // 1.关闭提交按钮忙等
                  this.editLoading = false;
                  if (result.data.success) {
                    // 1.给出成功提示
                    // 2.关闭弹出框
                    this.editFormVisible = false;
                    this.$message({
                      message: result.data.msg,
                      type: 'success'
                    });
                    // 2.修改页码为第一页
                    // this.currentPage = 1;
                    // 3.调用函数刷新列表
                    this.getOldMan();
                  } else {
                    // 1.给出失败提示
                    this.$message({
                      message: result.data.msg,
                      type: 'error'
                    });
                    this.getOldMan();
                  }
                })
          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.patch("/oldman", ids)
            .then(result => {
              result = result.data;
              if (result.success) { // 如果后端返回success
                this.$message({
                  message: result.msg,
                  type: 'success'
                });
                this.currentPage = 1,
                    // 刷新列表
                    this.getOldMan();
              } else {
                this.$message({
                  message: result.msg,
                  type: 'error'
                });
              }

            })
            .catch(result => { // 接口调用失败
              // 1.关闭忙等框
              this.listLoading = false;
              // 2.给出自定义失败提示！
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getOldMan();
    this.getOldManSex();
    this.getOldManPhysical();
    this.getBeds();
    this.getNurses();
  }
}

</script>

<style scoped>
.el-form-item{
  margin-bottom: 10px;
}
</style>